package com.wnhz.work.jy.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wnhz.work.jy.MyMain;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.activity.SearchActivity;
import com.wnhz.work.jy.adapter.ProductAdapter;
import com.wnhz.work.jy.fragment.good.AllGoodFragment;
import com.wnhz.work.jy.fragment.good.DeriveFragment;
import com.wnhz.work.jy.fragment.good.ForeignFragment;
import com.wnhz.work.jy.fragment.good.PandaFragment;
import com.wnhz.work.jy.fragment.good.SouvenirFragment;

import org.xutils.x;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment3 extends Fragment implements View.OnClickListener {

    private View view;
    private TextView title;
    private TabLayout tabLayout;
    private ViewPager vp_product;
    private FragmentPagerAdapter mAdapter;

    private List<android.support.v4.app.Fragment> list_fragment; //定义要装fragment的列表
    private List<String> list_title; //tab名称列表

    //    private RecommendFragment fragment1; //推荐
    private AllGoodFragment fragment1;
    private PandaFragment fragment2; //熊猫
    private SouvenirFragment fragment3; //纪念
    private DeriveFragment fragment4; //衍生品
    private ForeignFragment fragment5; //外国币

    private int position;
    private MyMain myMain;

    private View btn_main_right;
    private ImageView view_main_right_image;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.activity_product, container, false);

        myMain = (MyMain) getActivity();
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    public void jump(int i){

    }

    private void initView() {
        title = (TextView) view.findViewById(R.id.view_main_middle_title);
        title.setText("产品中心");

        tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
        vp_product = (ViewPager) view.findViewById(R.id.vp_product);

        view.findViewById(R.id.btn_main_left).setVisibility(View.GONE);

        btn_main_right = view.findViewById(R.id.btn_main_right);
        btn_main_right.setVisibility(View.VISIBLE);
        btn_main_right.setOnClickListener(this);
        view_main_right_image = (ImageView) view.findViewById(R.id.view_main_right_image);
        view_main_right_image.setVisibility(View.VISIBLE);
        view_main_right_image.setImageResource(R.mipmap.ben_sousuo);
       /* findViewById(R.id.btn_main_right).setVisibility(View.VISIBLE);
        findViewById(R.id.view_main_right_image).setVisibility(View.VISIBLE);
        findViewById(R.id.btn_main_right).setOnClickListener(this);*/

        //初始化各fragment
//        fragment1 = new RecommendFragment();
        fragment1 = new AllGoodFragment();
        fragment2 = new PandaFragment();
        fragment3 = new SouvenirFragment();
        fragment4 = new DeriveFragment();
        fragment5 = new ForeignFragment();

        //将fragment装进列表中
        list_fragment = new ArrayList<>();
        list_fragment.add(fragment1);
        list_fragment.add(fragment2);
        list_fragment.add(fragment3);
        list_fragment.add(fragment4);
        list_fragment.add(fragment5);

        //将名称加载tab名字列表
        list_title = new ArrayList<>();
        list_title.add("全部");
        list_title.add("熊猫币");
        list_title.add("纪念币");
        list_title.add("衍生品");
        list_title.add("外国币");

        //设置TabLayout的模式
        tabLayout.setTabMode(TabLayout.MODE_FIXED);

        //为TabLayout添加tab名称
        for (int i = 0; i < list_title.size(); i++) {
            tabLayout.addTab(tabLayout.newTab().setText(list_title.get(i)));
        }
        /*tabLayout.addTab(tabLayout.newTab().setText(list_title.get(0)));
        tabLayout.addTab(tabLayout.newTab().setText(list_title.get(1)));
        tabLayout.addTab(tabLayout.newTab().setText(list_title.get(2)));
        tabLayout.addTab(tabLayout.newTab().setText(list_title.get(3)));*/
//        tabLayout.addTab(tabLayout.newTab().setText(list_title.get(4)));

        mAdapter = new ProductAdapter(getActivity().getSupportFragmentManager(), list_fragment, list_title);

        //viewpager加载adapter
        vp_product.setAdapter(mAdapter);

        //TabLayout加载viewpager
        tabLayout.setupWithViewPager(vp_product);
        vp_product.setOffscreenPageLimit(list_title.size());

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_right:
                startActivity(new Intent(getActivity(), SearchActivity.class));
                break;
            default:
                break;
        }
    }
}
